﻿using OrderSystem.Common;
using OrderSystem.DataAccess;
using OrderSystem.Templet;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace OrderSystem
{
    public partial class FormQueryOperator : FormTemplet
    {
        public FormQueryOperator()
        {
            this.InitializeComponent();
        }

        private void FormQueryOperator_Load(object sender, EventArgs e)
        {
            this.cbCondition.SelectedIndex = 0;
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            string text = "SELECT op.PKID as 序号,op.[Name] as 姓名,op.Sex as 性别,op.Phone as 电话,op.Address as 地址,op.Email as 邮件,op.LoginName as 登陆名,op.Password as 密码,ro.[Name] as 角色,op.Remark as 备注 FROM Operator as op inner join Role as ro on op.RoleID=ro.PKID";
            if (this.cbCondition.Text == this.cbCondition.Items[0].ToString())
            {
                text = text + " WHERE op.[Name] LIKE '%" + this.txtCondition.Text.Trim() + "%'";
            }
            if (this.cbCondition.Text == this.cbCondition.Items[1].ToString())
            {
                text = text + " WHERE op.Phone LIKE '%" + this.txtCondition.Text.Trim() + "%'";
            }
            if (this.cbCondition.Text == this.cbCondition.Items[2].ToString())
            {
                text = text + " WHERE op.Address LIKE '%" + this.txtCondition.Text.Trim() + "%'";
            }
            if (this.cbCondition.Text == this.cbCondition.Items[3].ToString())
            {
                text = text + " WHERE op.Email LIKE '%" + this.txtCondition.Text.Trim() + "%'";
            }
            if (this.cbCondition.Text == this.cbCondition.Items[4].ToString())
            {
                text = text + " WHERE op.LoginName LIKE '%" + this.txtCondition.Text.Trim() + "%'";
            }
            Operators operators = new Operators();
            DataTable dataTable = null;
            try
            {
                dataTable = operators.GetOperators(text);
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            catch (Exception ex2)
            {
                MessageBox.Show(ex2.Message + "\r\n获取数据时出现错误，请从试", "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            operators.Dispose();
            if (dataTable != null)
            {
                this.dgOperator.DataSource = null;
                this.dgOperator.DataSource = dataTable;
                this.dgOperator.Columns["密码"].Visible = false;
                this.dgOperator.AutoResizeColumns();
                dataTable.Dispose();
            }
        }

        private void btnUpdateOperator_Click(object sender, EventArgs e)
        {
            if (!LoginInfo.Role[PowerName.User_Update])
            {
                MessageBox.Show("你没有操作这里的权限！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else if (this.dgOperator == null || this.dgOperator.Rows.Count < 1)
            {
                MessageBox.Show("表格中没有数据", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
            {
                int rowIndex = this.dgOperator.CurrentCell.RowIndex;
                new FormUpdateOperator(Convert.ToInt32(this.dgOperator.Rows[rowIndex].Cells[0].Value))
                {
                    MdiParent = base.MdiParent
                }.Show();
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            base.Close();
        }
    }
}
